<template>
	<view class="">
		<view class="content">
			<!-- 头部banner -->
			<!-- <detail-swiper :list="swiperList"></detail-swiper> -->

			<view class="top-bg">

			</view>

			<view class="content_info">
				<!-- 活动具体信息 -->
				<detail-info :activityTitle="activityInfo.luckdrawName" :finishTime="activityInfo.activityTime"
					:bottomTimeShow="false">
					<view class="other-content" slot="other" v-if="activityInfo.activityTime">
						活动时间:{{activityInfo.timeStr}}
					</view>
				</detail-info>
				<!-- 活动奖品 -->
				<detail-prize :prizeList="activityInfo.peerGsList"></detail-prize>
				<!-- 组队成员 -->
				<detail-group-team :numberClusters="activityInfo.numberClusters"
					:noNumberClusters="activityInfo.noNumberClusters" :status="activityInfo.status"
					:customerVoList="activityInfo.customerVoList" :isAgree="isAgree" :activityId="activityId"
					:activityName="activityInfo.luckdrawName" :fsUrl="baseImageUrl+activityInfo.fsUrl"
					:toCustomerNo="toCustomerNo" @share="shareActivity" @agreenTeam="agreenTeam"></detail-group-team>
				<!-- 获得奖品 -->
				<view v-if="activityInfo.obtainAwardList.length > 0">
					<detail-already-prize :prizeList="activityInfo.obtainAwardList"></detail-already-prize>
				</view>
				<!-- 活动门店 -->
				<view v-if="activityInfo.clientStoreDto">
					<home-title title="活动门店" type="2"></home-title>
					<view style="margin-bottom: 20upx;" v-for="(item, index) in activityInfo.clientStoreDto"
						:key="index">
						<store-cell :isLocation="0" :storeName="item.applicableUnit" :storeAddress="item.unitAddress"
							:distance="item.distance" :phone="item.unitPhone" :latitude="item.unitLat"
							:longitude="item.unitLon"></store-cell>
					</view>
				</view>
				<!-- 活动介绍 -->
				<detail-introduce :richInfo="activityInfo.content"></detail-introduce>
			</view>
		</view>
		<login-mask></login-mask>
	</view>
</template>

<script>
	import detailSwiper from "../../components/activity/activity-detailSwiper.vue"
	import detailInfo from '../../components/activity/activity-detailInfo.vue'
	import homeTitle from '../../components/layout/mmv-home-title.vue'
	import storeCell from '../../components/coupon/store-cell.vue'
	import detailPrize from '../../components/activity/activity-detailPrize.vue'
	import detailGroupTeam from '../../components/activity/activity-detailGroupTeam.vue'
	import detailAlreadyPrize from '../../components/activity/activity-detailAlreadyPrize.vue'
	import detailIntroduce from '../../components/activity/activity-detailIntroduce.vue'
	import loginMask from '@/components/layout/login-mask.vue'
	import {
		clientPeerInfo,
		insertActivityBrowseRecords,
		invitedUsers
	} from '../../common/api.js'
	import GLOBAL_CONFIG from '@/common/config';
	import moment from '@/util/moment.js'
	
	export default {
		components: {
			detailSwiper,
			detailInfo,
			homeTitle,
			storeCell,
			detailPrize,
			detailGroupTeam,
			detailAlreadyPrize,
			detailIntroduce,
			loginMask
		},
		data() {
			return {
				time: null,
				timeNumber: 0,
				popupShow: false,
				lat: '',
				lon: '',
				activityId: '',
				activityInfo: {
					// customerVoList:[],
					//       "id": "1",
					//       "luckdrawName": "测试",
					//       "endTime": "2天23小时",
					//       "salesNumber": 0,
					//       "activityTime": "1640941499",
					//       "timeStr": "2021-12-28至2021-12-31",
					//       "content": "123.png",
					//       "fsUrl": "123.png",
					//       "clientStoreDto": [
					//           {
					//               "applicableUnitNo": "1",
					//               "applicableUnit": "测试一部",
					//               "unitAddress": "陕西西安",
					//               "unitPhone": "13892006829",
					//               "distance": 0.0,
					//               "serviceConsultant": 0
					//           }
					//       ],
					//       "peerGsList": [
					//           {
					//               "id": "1",
					//               "peerId": "1",
					//               "peerName": "奖品1",
					//               "peerNumber": 1,
					//               "peerUrl": 'uni_rental/home_couponBg.png',
					//               "peerStock": 10
					//           },
					//           {
					//               "id": "67",
					//               "peerId": "1",
					//               "peerName": "奖品2",
					//               "peerNumber": 1,
					//               "peerUrl": 'uni_rental/home_couponBg.png',
					//               "peerStock": 10
					//           },
					//           {
					//               "id": "67",
					//               "peerId": "1",
					//               "peerName": "奖品2",
					//               "peerNumber": 1,
					//               "peerUrl": 'uni_rental/home_couponBg.png',
					//               "peerStock": 10
					//           },
					//           {
					//               "id": "67",
					//               "peerId": "1",
					//               "peerName": "奖品2",
					//               "peerNumber": 1,
					//               "peerUrl": 'uni_rental/home_couponBg.png',
					//               "peerStock": 10
					//           },
					//           {
					//               "id": "67",
					//               "peerId": "1",
					//               "peerName": "奖品2",
					//               "peerNumber": 1,
					//               "peerUrl": 'uni_rental/home_couponBg.png',
					//               "peerStock": 10
					//           }
					//       ],
					//       "customerVoList": [
					//           {
					//               "vxUrl": this.baseImageUrl+"uni_rental/同行头像2x.png",
					//               "vxName": "张三",
					//               "customerNo": "123"
					//           },
					//           {
					//               "vxUrl": this.baseImageUrl+"uni_rental/消息通知@2x.png",
					//               "vxName": "张三",
					//               "customerNo": "123"
					//           },
					//           {
					//               "vxUrl": this.baseImageUrl+"uni_rental/我的_默认_icon@2x.png",
					//               "vxName": "张三",
					//               "customerNo": "123"
					//           },
					//           {
					//               "vxUrl": this.baseImageUrl+"uni_rental/同行头像2x.png",
					//               "vxName": "张三",
					//               "customerNo": "123"
					//           },
					//           {
					//               "vxUrl": this.baseImageUrl+"uni_rental/同行头像2x.png",
					//               "vxName": "张三",
					//               "customerNo": "123"
					//           },
					//           // {
					//           //     "vxUrl": this.baseImageUrl+"uni_rental/同行头像2x.png",
					//           //     "vxName": "张三",
					//           //     "customerNo": "123"
					//           // },
					//           // {
					//           //     "vxUrl": this.baseImageUrl+"uni_rental/同行头像2x.png",
					//           //     "vxName": "张三",
					//           //     "customerNo": "123"
					//           // },
					//           // {
					//           //     "vxUrl": this.baseImageUrl+"uni_rental/同行头像2x.png",
					//           //     "vxName": "张三",
					//           //     "customerNo": "123"
					//           // },

					//       ],
					//       "obtainAwardList": [{
					// 	peerUrl:this.baseImageUrl+"uni_rental/同行头像2x.png",
					// 	name:'牙刷高级电动不用你手刷'
					// },{
					// 	peerUrl:this.baseImageUrl+"uni_rental/同行头像2x.png",
					// 	name:'昆明的覅欧工农商当年法国岁的法国'
					// },{
					// 	peerUrl:this.baseImageUrl+"uni_rental/同行头像2x.png",
					// 	name:'个如果违反'
					// }],
					//       "numberClusters": "2人团",
					//       "noNumberClusters": "还差1人获得奖品1",
					//       "status": 1
				},
				swiperList: [],
				toCustomerNo: '',
				// isAgree: false,
				baseImageUrl: GLOBAL_CONFIG.yxBaseImageUrl,
				orderno:'',
				turnoverType:'',
				// customerNo: null
			}
		},

		computed: {
			customerNo() {
				return this.$store.state.user.userInfo.customerId;
			},
			isAgree() {
				let that = this;
				return this.activityInfo.customerVoList.filter(function(a) {
					return that.customerNo == a.customerNo
				}).length == 0
			},
			isNotLogin() {
				return uni.$u.test.isEmpty(this.$store.state.user.userInfo);
			}
		},
		// watch:{
		// 	customerNo() {
		// 		this.getLocation();
		// 		this.reckonBytime()
		// 	}
		// },
		onLoad(op) {
			
			//记录开始时间
			getApp().globalData.taskStartTime = new Date().getTime();
			
			// console.log('开始时间', moment(getApp().globalData.taskStartTime).format('YYYY-MM-DD HH:mm:ss'));
			
			getApp().globalData.objType = 3;
			
			
			if(!uni.$u.test.isEmpty(op.scene)){
				const scene = decodeURIComponent(op.scene);
				let s_list = scene.split('=');
				console.log('op-scene=>', s_list);
				
				//任务id=推广人id=推广人类型=推广活动id
				getApp().globalData.taskObjId = s_list[0];
				getApp().globalData.personnelId = s_list[1];
				getApp().globalData.personnelType = s_list[2];
				op.bizNo = s_list[3];
			}
			
			this.activityId = op.bizNo;
			getApp().globalData.objId = this.activityId;
			
			this.toCustomerNo = op.toCustomerNo || this.customerNo;

			console.log('收到的deptId=>', op.deptId);
			console.log('收到的toCustomerNo=>', this.toCustomerNo);
			console.log('自己的customerNo=>', this.customerNo);

			getApp().globalData.tempDeptId = op.deptId;
			if (!uni.$u.test.isEmpty(op.deptId)) {
				this.$store.commit('user/saveSourceType', 4);
				this.$store.commit('user/savedeptId', op.deptId);
				
				this.$store.dispatch('app/getStoreInfo', op.deptId);
			}

			getApp().globalData.tempCustomerId = op.customerId;
			if (!uni.$u.test.isEmpty(op.customerId)) {
				this.$store.commit('user/saveCustomerId', op.customerId);
			}

			// this.reckonBytime();


			// if (!this.isNotLogin) {
			// 	console.log('登录了');
			this.getLocation();
			// } else {
			// 	this.$store.dispatch('user/MmvLogin')
			// }

			uni.$on('login', res => {
				console.log('没登陆，登录成功了');
				this.toCustomerNo = op.toCustomerNo || this.customerNo;
				this.getLocation();
			})
			
			
			if(uni.$u.test.isEmpty(this.$store.state.user.openid)){
				//如果openid是空，可能是从扫码直接进入的，得等openid那个接口请求完了收到通知，再调操作记录接口
				uni.$on('getOpenIded', res=>{
					console.log('我收到openid了');
					
					//访客
					this.$store.dispatch('home/recordUse', {
						recordType:2,
					})
				})
			}else{
				//访客
				this.$store.dispatch('home/recordUse', {
					recordType:2,
				})
			}

		},
		beforeDestroy() {
			// this.insertActivityBrowseRecords()
			// clearInterval(this.time)
		},
		methods: {
			insertActivityBrowseRecords() {
				insertActivityBrowseRecords({
					bizNo: this.activityId,
					memberNo: this.customerNo,
					browseDuration: this.time,
					tenantId: this.$store.state.user.tenantCode,
					activityType: 3
				}).then(res => {})
			},
			reckonBytime() {
				this.time = setInterval(() => {
					this.timeNumber++
				}, 1000)
			},
			//获取定位信息
			getLocation() {
				uni.getLocation({
					type: 'wgs84',
					success: (res => {
						console.log(res.latitude);
						this.lat = res.latitude;
						this.lon = res.longitude;
						this.getData();
					}),
					fail: (e => {
						this.getData();
					})
				});
			},
			//获取详情数据
			getData() {
				let that = this;
				clientPeerInfo({
					bizNo: that.activityId,
					lon: this.lon,
					lat: this.lat,
					memberNo: that.toCustomerNo || that.customerNo
				}).then(res => {
					this.activityInfo = res;
					// that.isAgree =res.customerVoList.filter(function (a){
					//    return that.peerInviter ==a.customerNo
					// }).length==0
					if (this.activityInfo.detailsImage.length > 0) {
						let arr = this.activityInfo.detailsImage.split(',');
						this.swiperList = arr.map(item => {
							return GLOBAL_CONFIG.yxBaseImageUrl + item;
						})
					}
					
					if(!uni.$u.test.isEmpty(res.orderId)){
						//这里肯定是发起人第一次发起了组队
						this.turnoverType = 1;
						this.orderno = res.orderId;
						this.recordOrder();
					}
					
					//如果门店id是空，从详情获取使用门店第一个
					if (uni.$u.test.isEmpty(this.$store.state.user.deptId)) {
						getApp().globalData.tempDeptId = res.clientStoreDto[0].applicableUnitNo;
						this.$store.commit('user/saveSourceType', 2);
						this.$store.commit('user/savedeptId', res.clientStoreDto[0].applicableUnitNo);
					
						this.$store.dispatch('app/getStoreInfo', res.clientStoreDto[0].applicableUnitNo);
					}
					
					
					
				})
			},
			onShareAppMessage(res) {
				this.$store.dispatch('home/recordShare')

				return {
					title: this.activityInfo.luckdrawName,
					imageUrl: this.baseImageUrl + this.activityInfo.fsUrl,
					path: '/pages/activity/activityDetail-peer?bizNo=' + this.activityId + '&toCustomerNo=' +
						this.toCustomerNo + '&deptId=' + this.$store.state.user.deptId + '&customerId=' + this.$store.state
						.user.userInfo.customerId

				}
			},

			//同意组队
			agreenTeam() {

				if (this.isNotLogin) {
					this.$store.dispatch('user/MmvLogin')
					return
				}


				invitedUsers({
					activityId: this.activityId,
					peerInviter: this.toCustomerNo,
					peerBeInviter: this.customerNo
				}).then(res => {
					uni.showToast({
						title: '组队成功',
						icon: 'success'
					})
					
					//这里肯定是非发起人参与了组队
					this.turnoverType = 2;
					this.orderno = res;
					this.getData();
					this.recordOrder();
				})
			},
			
			//下单操作记录
			async recordOrder(){
				console.log('进入操作记录了');
				
				let nowTimes = new Date().getTime();
				
				let duration = Math.round((nowTimes - getApp().globalData.taskStartTime)/1000);
				
				//访问
				this.$store.dispatch('home/recordUse', {
					recordType:4,
					recordOrderId:this.orderno,
					objRecordData:{
						durationTime:duration,
						recordTime:moment(nowTimes).format('YYYY-MM-DD HH:mm:ss'),
						price:this.amount,
						turnoverType:this.turnoverType,
						initiatorCustomerId:this.toCustomerNo,
						participationCustomerId:this.customerNo
					}
				})
			},
		},
		onShareTimeline() {
			this.$store.dispatch('home/recordShare')

			return {
				title: this.activityInfo.luckdrawName,
				imageUrl: this.baseImageUrl + this.activityInfo.fsUrl,
				path: '/pages/activity/activityDetail-peer?bizNo=' + this.activityId + '&toCustomerNo=' + this
					.toCustomerNo + '&deptId=' + this.$store.state.user.deptId + '&customerId=' + this.$store.state.user
					.userInfo.customerId
			}
		},
		onUnload() {
			
			let nowTimes = new Date().getTime();
			
			let duration = Math.round((nowTimes - getApp().globalData.taskStartTime)/1000);
			
			
			//访问
			this.$store.dispatch('home/recordUse', {
				recordType:3,
				objRecordData:{
					durationTime:duration,
					recordTime:moment(nowTimes).format('YYYY-MM-DD HH:mm:ss')
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		&_info {
			padding: 0 30upx;
			position: relative;
			top: -90upx;

		}
	}

	.top-bg {
		width: 100%;
		height: 152upx;
		background: linear-gradient(to bottom, #F5295E, #EF7091, #F8F8F8);

	}

	.other-content {
		font-size: 20upx;
		font-family: PingFang SC, PingFang SC-Medium;
		font-weight: bold;
		text-align: left;
		color: #232323;
	}
</style>
